package de.gzork.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Stefcom72 implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while " + "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GzorkServiceAsync greetingService = GWT.create(GzorkService.class);

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {
		final Button sendButton = new Button("Send");
		final TextBox commandField = new TextBox();
		final Label warningLabel = new Label("Please login to facebook!");

		commandField.setText("");
		commandField.setFocus(true);

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("commandFieldContainer").add(commandField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("warningContainer").add(warningLabel);

		RootPanel.get("commandRow").setVisible(false);

		final Label messageLabel = new Label();
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		messageLabel.setText("");
		messageLabel.setSize("400px", "200px");
		decoratorPanel.setWidth("400px");
		decoratorPanel.setHeight("200px");
		decoratorPanel.add(messageLabel);

		RootPanel.get("messageContainer").add(decoratorPanel);

		greetingService.facebookLogin(Window.Location.getParameter("auth_token"), new AsyncCallback<String>()  {

			@Override
			public void onFailure(Throwable caught) {
				sendButton.setVisible(false);
				warningLabel.setVisible(true);
				if(caught.getMessage().equalsIgnoreCase("redirect!")) {					
					Window.Location.replace("http://www.facebook.com/login.php?v=1.0&api_key=4153a065027a515064a75ccbd317e7f1&next=gzork&canvas=true");
				} 
			}

			@Override
			public void onSuccess(String result) {
				warningLabel.setVisible(false);
				
				if (result != null && !result.isEmpty()) {
					messageLabel.setText(result);
					commandField.setText("");
					sendButton.setEnabled(true);
					RootPanel.get("commandRow").setVisible(true);
					commandField.setVisible(true);
					sendButton.click();
				} 
			}});
		
		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			
			private String _name = "test";

			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendCommandToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendCommandToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendCommandToServer() {
				sendButton.setEnabled(false);

				String command = commandField.getText();
				if (command.isEmpty()) {
					command = "look";
				}
				
				greetingService.greetServer(_name, command, new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						messageLabel.setText(caught.getLocalizedMessage());
					}

					public void onSuccess(String result) {
						messageLabel.setText(result);
						commandField.setText("");
						sendButton.setEnabled(true);
						RootPanel.get("commandRow").setVisible(true);
					}
				});
			}

		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		commandField.addKeyUpHandler(handler);
		
	}
}
